﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Transactions;
using Data.Contexto;
using Domain.Entidades;
using Domain.Interfaces.Repositorios;

namespace Data.Repositorios
{
    public class VW_noticiasRepository : RepositoryBase<VW_noticias>, IVW_noticiasRepository
    {

        protected credigeralContext Dbseg = new credigeralContext();


        public IEnumerable<VW_noticias> busca_noticias(DateTime data1, DateTime data2)
        {
            return  Db.VW_noticias.Where(x => x.not_data_inclusao <= data2 && x.not_data_inclusao >= data1).OrderByDescending(x => x.not_data_inclusao).ThenByDescending(x => x.not_codigo).ToList();
           
        }





        public IEnumerable<setor_email> setor_email()
        {
            return Db.setor_email.ToList();

        }


        public void Inserir(noticia not, VW_usuarios user)
        {

            try
            {


                using (TransactionScope scope = new TransactionScope())
                {


                    not.not_data_inclusao = DateTime.Now;
                    not.not_hora = DateTime.Now.ToString("t");
                    not.not_data_modificado = not.not_data_inclusao;
                    not.not_hora_modificado = not.not_hora;
                    not.not_set_modificado = user.set_codigo;
                    not.not_setor = user.set_codigo;


                    Db.noticias.Add(not);
                    Db.SaveChanges();


                    log logs = new log();
                    logs.log_modulo = 3;
                    logs.log_tipo = 1023;
                    logs.log_descricao = "Notícia codigo: " + not.not_codigo;
                    logs.log_usuario = user.usu_codigo;
                    logs.log_data = DateTime.Now;
                    logs.log_horario = DateTime.Now.ToString("T");
                    Dbseg.log.Add(logs);
                    Dbseg.SaveChanges();
                    scope.Complete();
                 


                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void Editar(noticia not,VW_usuarios usuario)
        {


            try
            {

                using (TransactionScope scope = new TransactionScope())
                {

                    not.not_data_modificado = DateTime.Now;
                    not.not_hora_modificado = DateTime.Now.ToString("t");
                    not.not_set_modificado = usuario.set_codigo;
                  

                    Db.Entry(not).State = EntityState.Modified;
                    Db.SaveChanges();


                    

                    log logs = new log();
                    logs.log_modulo = 3;
                    logs.log_tipo = 1023;
                    logs.log_descricao = "Notícia codigo: " + not.not_codigo;
                    logs.log_usuario = usuario.usu_codigo;
                    logs.log_data = DateTime.Now;
                    logs.log_horario = DateTime.Now.ToString("T");
                    Dbseg.log.Add(logs);
                    Dbseg.SaveChanges();
                    scope.Complete();


                }

            }

            catch (Exception ex)
            {

                throw ex;
            }
           
        }

        public void deletar(int codigo,VW_usuarios user)
        {
            try
            {

                using (TransactionScope scope = new TransactionScope())
                {



                    Db.noticias.Remove(Db.noticias.FirstOrDefault(x => x.not_codigo == codigo));
                    Db.SaveChanges();



                    log logs = new log();
                    logs.log_modulo = 3;
                    logs.log_tipo = 1025;
                    logs.log_descricao = "Notícia codigo: " + codigo;
                    logs.log_usuario = user.usu_codigo;
                    logs.log_data = DateTime.Now;
                    logs.log_horario = DateTime.Now.ToString("T");
                    Dbseg.log.Add(logs);
                    Dbseg.SaveChanges();
                

                    scope.Complete();

                }
            }




            catch (Exception ex)
            {

                throw ex;
            }
            
        }

        public noticia carrega_busca(int codigo)
        {
            try
            {

               
                noticia not = Db.noticias.FirstOrDefault(x => x.not_codigo == codigo);
                return not;

            }
            catch (Exception ex)
            {

                throw ex;
            }
       

        }

        public VW_noticias ver_noticia(int codigo)
        {
            try
            {


                VW_noticias not = Db.VW_noticias.FirstOrDefault(x => x.not_codigo == codigo);
                return not;

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }




    }
}
